﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TPWB02
    {     
      
       
        public TPWB02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBDAT();
            this.DataContext = MV;
            InitializeComponent(); 
        }

        public override void OnWGChg(DataRow rowWG)
        {
            if (rowWG == null)
                return;
            CART = rowWG["WGART"].ToString() + rowWG["WGVAR"].ToString();
            CFARBE = rowWG["WGFARBE"].ToString();
            MV.LO = rowWG["WGLO"].ToString();
            MV.YYYY = rowWG["WGMJJ"].ToString();
            MV.MM = rowWG["WGMM"].ToString();
            QueryCommand(null, null);
        }
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        { 
            dwData = TBMain.NewRow();
            dwData["WBMANDANT"] = app.Mandant;
            if (MV.YYYY.Trim()==""&&MV.MM.Trim()==""&&MV.DD.Trim()=="")
            {
                dwData["WBEINDAT"] = "2";
            }
            else
            {
                dwData["WBEINDAT"] = MV.YYYY.Trim() + MV.MM.Trim() + MV.DD.Trim();

            }
            dwData["WBEINLO"] = MV.LO;
            dwData["WBART"] = ART;
            dwData["WBVAR"] = VAR;
            dwData["WBFARBE"] = FARBE;
            SResult = TMV.QWBDAT_2(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            DataRow dwData;
            dwData = TBMain.Rows.Add(); 
            vwMaster.View.MoveCurrentToLast(); 
            base.NewCommand(sender, e);
        }

        public override void EditCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        } 

        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            PM["RWKRS"] = dwMaster["WBRWKRS"].ToString();
            PM["RWLFD"] = dwMaster["WBRWLFD"].ToString();
            TMV.DELWBDAT(PM);  
            filter = "WBRWKRS='" + dwMaster["WBRWKRS"].ToString() + "' AND WBRWLFD='" + dwMaster["WBRWLFD"].ToString() + "'";
            vwMaster.View.MoveCurrentTo(-1); 
            DataRow[] rows =TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        } 
         
      
        public override void OnReload(bool value)
        {
            if (!value)
                return;
            if (ORDERNUM.Length < 8)
                return;
            filter = "WBRWKRS='" + ORDERNUM.Substring(0, 2) + "' AND WBRWLFD='" + ORDERNUM.Substring(2, 6) + "'";
            rows = TBMain.Select(filter);
            if (rows.Length == 0)
                return;
            dwData = TBMain.NewRow();
            dwData["WBMANDANT"] = app.Mandant;
            dwData["WBFILLER2"] = " AND WBRWKRS='" + ORDERNUM.Substring(0, 2) + "' AND WBRWLFD='" + ORDERNUM.Substring(2, 6) + "'";
            SResult = ((VWBDAT)MV).QueryWBDAT(dwData, -2);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            if (TBTemp.Rows.Count == 0) return;
            rows[0].ItemArray = TBTemp.Rows[0].ItemArray;
            base.OnReload(value);
        }
         
    }
}
